package com.abm;

import com.abm.data.DatabaseManager;
import com.abm.dbclases.Client;
import com.abm.entity.Actividad;
import com.abm.entity.Util;
import android.os.Bundle;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddClientActivity extends Actividad {
	private Button btn_cancel;
	private Button btn_save;
	private Dialog dialog_confirmation;
	private Util utilman;
	private DatabaseManager manager;
	private EditText txt_name;
	private EditText txt_surname;
	private EditText txt_company;

	private Client client;
	private boolean newClient = false;

	public static String PATH_APPDIRs = "sistema_ventas";

	// public static String PATH_APPDIR_multimedia = "/.productsPhotos/"
	// + "registros/Multimedia/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_client);

		txt_name = (EditText) findViewById(R.id.addclient_edittext_name);
		txt_surname = (EditText) findViewById(R.id.addclient_edittext_surname);
		txt_company = (EditText) findViewById(R.id.addclient_edittext_company);

		utilman = Util.getInstanceUtil();
		manager = DatabaseManager.getInstance();

		if (getIntent().hasExtra("CLIENT_ID"))
			client = manager.getClientByID(getIntent().getIntExtra("CLIENT_ID",
					-1));
		else {
			client = new Client();
			newClient = true;
		}

		txt_name.setText(client.getName());
		txt_surname.setText(client.getSurname());
		txt_company.setText(client.getCompany());

		mContext = getApplicationContext();

		btn_cancel = (Button) findViewById(R.id.addclient_button_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btn_save = (Button) findViewById(R.id.addclient_button_save);
		btn_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				validate();
			}
		});

	}

	private void validate() {

		if (txt_name.getText().toString().equals("")
				|| txt_name.getText().toString().equals("")
				|| txt_surname.getText().toString().equals("")
				|| txt_company.getText().toString().equals("")) {
			dialog_confirmation = utilman
					.showDialog(
							getString(R.string.complete_all_required_fields_before_continue),
							getString(R.string.atention),
							AddClientActivity.this);
			try {
				dialog_confirmation.show();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {

			// String surname = txt_surname.getText().toString();
			// String company = txt_company.getText().toString();

			// client = new Client(txt_name.getText().toString(), surname,
			// company);
			client.setName(txt_name.getText().toString());
			client.setSurname(txt_surname.getText().toString());
			client.setCompany(txt_company.getText().toString());

			if (newClient)
				manager.addClient(client);
			else
				manager.updateClient(client);
		}

		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_client, menu);
		return true;
	}

}
